<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/1/16-9:35
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------


namespace app\admin\controller;


use app\admin\model\AdminUser;
use app\admin\model\Settings;
use app\admin\service\AdminUserService;
use app\admin\service\RoleService;
use app\admin\validate\Pass;
use Jrk\Util;
use think\Db;
use think\Exception;
use think\Validate;


class Setting extends AdminBase
{


    /**
     * 阿里云OSS上传点
     * @var array
     */
    protected $ossPoints = [
        'oss-cn-hangzhou.aliyuncs.com' => '华东 1 杭州',
        'oss-cn-shanghai.aliyuncs.com' => '华东 2 上海',
        'oss-cn-qingdao.aliyuncs.com' => '华北 1 青岛',
        'oss-cn-beijing.aliyuncs.com' => '华北 2 北京',
        'oss-cn-zhangjiakou.aliyuncs.com' => '华北 3 张家口',
        'oss-cn-huhehaote.aliyuncs.com' => '华北 5 呼和浩特',
        'oss-cn-shenzhen.aliyuncs.com' => '华南 1 深圳',
        'oss-cn-hongkong.aliyuncs.com' => '香港 1',
        'oss-us-west-1.aliyuncs.com' => '美国西部 1 硅谷',
        'oss-us-east-1.aliyuncs.com' => '美国东部 1 弗吉尼亚',
        'oss-ap-southeast-1.aliyuncs.com' => '亚太东南 1 新加坡',
        'oss-ap-southeast-2.aliyuncs.com' => '亚太东南 2 悉尼',
        'oss-ap-southeast-3.aliyuncs.com' => '亚太东南 3 吉隆坡',
        'oss-ap-southeast-5.aliyuncs.com' => '亚太东南 5 雅加达',
        'oss-ap-northeast-1.aliyuncs.com' => '亚太东北 1 日本',
        'oss-ap-south-1.aliyuncs.com' => '亚太南部 1 孟买',
        'oss-eu-central-1.aliyuncs.com' => '欧洲中部 1 法兰克福',
        'oss-eu-west-1.aliyuncs.com' => '英国 1 伦敦',
        'oss-me-east-1.aliyuncs.com' => '中东东部 1 迪拜',
    ];


    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model = new Settings();


    }


    /**
     * @return mixed|void
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/12 0012
     * @name: index
     * @describe:
     */
    public function index()
    {
        if (IS_POST) {
            $data = $this->request->post();
            $validate = new \app\admin\validate\AdminUser();
            if (!$validate->scene('edit')->check($data)) {
                $this->error($validate->getError());
            }
            addLog("修改管理员信息", "修改管理员信息:" . $data['username']);
            //dump($data);die;
            $admin_user = new AdminUser();

            return $admin_user->addAndEdit($data);
        }
        $info = AdminUserService::getOne($this->admin_id);
        $roleService = new RoleService();
        $role = $roleService->getOne(['id' => $this->system_auth]);
        $role_name = $role['name'];

        $this->assign(compact("info", 'role_name'));

        return $this->fetch();
    }


    /**
     * @return mixed
     * @throws Exception
     * @throws \think\exception\PDOException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/12 0012
     * @name: pass
     * @describe:
     */
    public function pass()
    {
        if (IS_POST) {
            $data = $this->request->post();

            $pass =po_pass($this->admin_info['password']);
            if ($pass != $data['oldpassword']) {
                $this->error("当前密码输入错误");
            }

            $validate = new Pass();
            //校验数据
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $data['password'] = password($data['password']);

            unset($data['oldpassword']);
            unset($data['repassword']);

            addLog("修改管理员密码", "修改管理员密码");

            $result = AdminUser::update($data);
            if ($result) {
                $this->success("修改成功");
            } else {
                $this->error("修改失败");
            }

        }

        return $this->fetch();
    }


    /**
     * @return mixed
     * @throws Exception
     * @throws \think\exception\PDOException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/12 0012
     * @name: safe
     * @describe:
     */
    public function safe()
    {
        if (IS_POST) {
            $data = $this->request->post();
            foreach ($data as $k => $v) {
               Db::name("setting")->where("key", $k)->setField("value", $v);
            }
            addLog("修改设置", "修改系统安全设置");
            $this->success("提交成功");
        }
        return $this->fetch();
    }


    /**
     * @return mixed
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/2/27
     * @name: system
     * @describe:系统设置
     */
    public function system()
    {
        $safe = $this->model->getSetting();
        //网站设置
        $this->assign("info", $safe);

        return $this->fetch();
    }


    /**
     * @return mixed
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/6
     * @name: store
     * @describe: 存储配置
     */
    public function store()
    {

        //oss
        $this->assign("ossPoints", $this->ossPoints);

        return $this->fetch();
    }

    /**
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/2/27
     * @name: emailPost
     * @describe:邮件设置
     */
    public function emailPost()
    {
        if (IS_POST) {
            $data = $this->request->post();
            try {
                foreach ($data as $k => $v) {
                    Db::name("setting")->where("key", $k)->setField("value", $v);
                }
                addLog("修改设置", "修改邮件设置");
                $this->success("提交成功");
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }
    }


    /**
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/2/27
     * @name: sitePost
     * @describe:网站设置提交
     */
    public function sitePost()
    {
        if (IS_POST) {
            $data = $this->request->post();
            try {
                foreach ($data as $k => $v) {
                    Db::name("setting")->where("key", $k)->setField("value", $v);
                }
                $this->success("提交成功");
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }
    }


    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/14 0014
     * @name: setList
     * @describe:配置值列表
     */
    public function setList()
    {
        if (IS_AJAX) {
            $data = Db::name("setting")->page(PAGE)->limit(LIMIT)->select();
            if (!empty($data)) {
                foreach ($data as $k => $v) {
                    $key = $v['key'];
                    $g = "GetSysConfig('" . $key . "')";
                    $p = "{:GetSysConfig('" . $key . "')}";
                    $data[$k]['copy'] = "php中：" . $g . "<a data-clipboard-text=" . $g . " class=\"copy layui-btn layui-bg-gray\" title=\"点击复制\" style=\"top:0;right:0;background:none!important\"><i class=\"fa fa-copy\"></i></a><br/>
模板中：" . $p . "<a data-clipboard-text=" . $p . " class=\"copy layui-btn layui-bg-gray\" title=\"点击复制\" style=\"top:0;right:0;background:none!important\"><i class=\"fa fa-copy\"></i></a>";
                }
            }
            $this->ajaxResult($data, 0, '', db("setting")->count());
        }
        return $this->fetch();
    }


    /**
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/14 0014
     * @name: change
     * @describe:修改值
     */
    public function change()
    {
        if (IS_POST) {
            $data = $this->request->post();
            try {
                $key = $data['key'];
                $type = (int)$data['type'];
                if ($type == 1) {
                    $res = Db::name("setting")->where(["key" => "$key"])->setField("value", $data['value']);
                } else {
                    $res = Db::name("setting")->where(["key" => "$key"])->setField("desc", $data['value']);
                }
                addLog("修改配置值", "修改【" . $key . "】的值为【" . $data['value'] . "】");
                if ($res !== false) {
                    $this->success("操作成功");
                } else {
                    $this->error("操作失败");
                }
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }
    }


    /**
     * @return mixed
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/3/14 0014
     * @name: addSet
     * @describe:添加配置项
     */
    public function addSet()
    {
        if (IS_POST) {
            $data = $this->request->post();
            try {
                $validate = Validate::make([
                    'key' => 'require|alphaDash|max:25',
                    'value' => 'require'
                ]);
                if (!$validate->check($data)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                $res = Db::name("setting")->insert($data);
                addLog("添加配置", "添加【" . $data['key'] . "】的值为【" . $data['value'] . "】");
                if ($res !== false) {
                    $this->success("添加成功");
                } else {
                    $this->error("添加失败");
                }
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }
        return $this->fetch();
    }


    /**
     * @return mixed
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/1/16
     * @name: alertSkin
     * @describe:皮肤动画设置
     */
    public function alertSkin()
    {
        return $this->fetch();
    }


}